﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rssc.App_Data;
using Rssc.administrator.app_data;
using System.Web.Services;

namespace Rssc.administrator
{
    public partial class login : System.Web.UI.Page
    {
        private static DbRsscDataContext context = new DbRsscDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            Logout();
        }

        private void Logout()
        {
            Session.Clear();
        }

        [WebMethod]
        public static data RsscLogin(string username, string password)
        {
            data _data = new data();

            var _user = (from u in context.rssc_users
                         where u.username.Equals(username)
                         select new { u.username, u.password }).SingleOrDefault();
            if (_user != null)
            {
                if (CPublic.Decode(password, _user.password))
                {
                    _data.valid = true;
                    _data.redirect = "default.aspx";
                    _data.error = "Success";
                }
                else
                {
                    _data.valid = false;
                    _data.redirect = "login.aspx";
                    _data.error = "Password failed";
                }
            }
            else
            {
                _data.valid = false;
                _data.redirect = "login.asx";
                _data.error = "Username failed";
            }
            
            return _data;
        }
    }
}